﻿Partial Public Class PlanificationEvenement
    Public Enum TypeEvenement
        Activite = 1
        Accompagnement = 3
        Tache = 2
    End Enum

    Private oTraductionEnMot As String
    Public ReadOnly Property Traduction() As String
        Get
            Try
                Dim oStringHeure As String
                oStringHeure = "De " + CType(Heure_Debut, Date).ToString("HH:mm") + " à " + CType(Heure_Fin, Date).ToString("HH:mm")
                If Not IsNothing(Le) Then
                    Return oStringHeure + " le " + CType(Le, Date).ToString("dd/MM/yyyy")
                End If
                If Not IsNothing(ToutLes) Then
                    Dim oString As String = ""
                    Dim i As Integer = 0
                    For Each C As Char In ToutLes
                        If C = "1" Then
                            Select Case i
                                Case 0
                                    oString += "Lundi,"
                                Case 1
                                    oString += "Mardi,"
                                Case 2
                                    oString += "Mercredi,"
                                Case 3
                                    oString += "Jeudi,"
                                Case 4
                                    oString += "Vendredi,"
                                Case 5
                                    oString += "Samedi,"
                                Case 6
                                    oString += "Dimanche,"
                            End Select
                        End If
                        i += 1
                    Next
                    oString = oString.Substring(0, oString.Length - 1)
                    Return oStringHeure + " tous les " + oString + " à chaque " + IIf(NbrSemaine = 1, "semaine.", NbrSemaine.ToString + " semaines.")
                End If
                If TousLesJours Then
                    Return oStringHeure + " tous les jours"
                End If
                If Not IsNothing(TousLes) Then
                    Return oStringHeure + " tous les " + TousLes.ToString + " du mois."
                End If


                Return ""
            Catch ex As Exception
                Return "Erreur"
            End Try
        End Get
    End Property

    Public Function CheckIfIsTheDay(ByVal DateToCheck As Date) As Boolean
        If CType(Me.APartirDe, Date).Date <= DateToCheck.Date Then
            If Not IsNothing(Me.TousLesJours) AndAlso Me.TousLesJours Then 'Si c'est tous les jours
                Return True
            End If

            If Not IsNothing(Me.Le) AndAlso CType(Me.Le, Date).Date = DateToCheck.Date Then
                Return True
            End If


            If Not IsNothing(Me.TousLes) AndAlso CType(Me.TousLes, Integer) = DateToCheck.Day Then
                Return True
            Else
                If Not IsNothing(Me.TousLes) AndAlso CInt(Me.TousLes) > DateToCheck.Day Then
                    If DateToCheck.AddDays(1).Month <> DateToCheck.Month Then
                        Return True
                    End If
                End If
            End If


        Else 'Si à partir de n'est pas encore arrivé
            Return False
        End If

        If Not IsNothing(Me.ToutLes) Then
            With Me
                Select Case DateToCheck.DayOfWeek
                    Case DayOfWeek.Monday
                        If .ToutLes(0) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                    Case DayOfWeek.Tuesday
                        If .ToutLes(1) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                    Case DayOfWeek.Wednesday
                        If .ToutLes(2) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                    Case DayOfWeek.Thursday
                        If .ToutLes(3) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                    Case DayOfWeek.Friday
                        If .ToutLes(4) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                    Case DayOfWeek.Saturday
                        If .ToutLes(5) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                    Case DayOfWeek.Sunday
                        If .ToutLes(6) = "1" Then
                            Return IsRightWeek(DateToCheck)
                        End If
                End Select
            End With


        End If




    End Function

    Private Function IsRightWeek(ByVal DateToCheck As Date) As Boolean
        If (FindWeek(DateToCheck) - FindWeek(Me.APartirDe)) Mod Me.NbrSemaine = 0 Then
            Return True
        End If
    End Function



    Function FindWeek(ByVal dtmDate As Date) As Integer
        Dim intThisYear, intThisMonth, x As Integer
        Dim dtmFirstDayOfWeek, FirstDayOfYear As Date


        ' Find first day of week

        Do While dtmDate.DayOfWeek <> 1
            intThisYear = dtmDate.Year
            intThisMonth = dtmDate.Month

            dtmDate = dtmDate.AddDays(-1)
        Loop
        dtmFirstDayOfWeek = dtmDate

        'Find week of year from first day of week
        FirstDayOfYear = CDate("01.01." & dtmFirstDayOfWeek.ToString("yyyy"))
        x = 0
        If FirstDayOfYear.DayOfWeek = 5 Or FirstDayOfYear.DayOfWeek = 6 Or FirstDayOfYear.DayOfWeek = 0 Then
            For x = 0 To dtmFirstDayOfWeek.DayOfYear + 1
            Next x
            x = (x / 7)
        Else
            For x = 1 To dtmFirstDayOfWeek.DayOfYear
            Next x
            x = (x / 7) + 1
            If dtmFirstDayOfWeek.Month = 12 Then
                If dtmFirstDayOfWeek.Day = 29 Then x = 1
                If dtmFirstDayOfWeek.Day = 30 Then x = 1
                If dtmFirstDayOfWeek.Day = 31 Then x = 1
            End If
        End If
        Return x
    End Function

End Class
